package com.tos.quran;

import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ViewAnimator;

import com.bappi.utils.Constants;
import com.custom.tab.AbstractTabRootManager;
import com.quran.viewcontrollers.SelectTranslationController;
import com.quran.viewcontrollers.Tab1Controller;
import com.quran.viewcontrollers.Tab2Controller;
import com.quran.viewcontrollers.Tab3Controller;
import com.quran.viewcontrollers.Tab4Controller;

public class QuranTRM extends AbstractTabRootManager {

	public QuranTRM(QuranActivity activity, LayoutInflater layoutInflater,
			ViewAnimator viewAnimator, int index) {
		super(activity, layoutInflater, viewAnimator, index);
		try {
			switch (index) {
			case 0:
				handleView();
				break;
			case 1:
				pushViewController(new Tab2Controller(this));
				break;
			case 2:
				pushViewController(new Tab3Controller(this));
				break;
			case 3:
				pushViewController(new Tab4Controller(this));
				break;
			default:
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		try {
			Log.i("DREG", "key=" + key);

			if (Constants.KEY_SELECTED_TRANS_CODE.equals(key)) {
				if (getIndex() == 0) {
					getActivity().setSelectedTab(0);
					handleView();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.onSharedPreferenceChanged(sharedPreferences, key);
	}

	private void handleView() {
		try {
			String selectedTranslation = getSharedPreferences().getString(
					Constants.KEY_SELECTED_TRANS_CODE, null);
			if ((selectedTranslation != null)
					&& (selectedTranslation.length() > 0)) {
				pushViewControllerClearingAll(new Tab1Controller(this));
			} else {
				pushViewControllerClearingAll(new SelectTranslationController(
						this));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
